package com.ssh.sina.api.weibo4j.examples;

import com.ssh.sina.api.weibo4j.Weibo;
import com.ssh.sina.api.weibo4j.WeiboException;
import com.ssh.sina.api.weibo4j.http.AccessToken;
import com.ssh.sina.api.weibo4j.http.ImageItem;
import com.ssh.sina.api.weibo4j.http.RequestToken;

public class WebOAuth {

   	private static final String CONSUMER_KEY = "3439810648";
	private static final String CONSUMER_SECRET = "5597e81c529c12ce2d619803a100e032";
    static{
        System.setProperty("weibo4j.oauth.consumerKey", CONSUMER_KEY);
		System.setProperty("weibo4j.oauth.consumerSecret",CONSUMER_SECRET);
    }

	/**
	 * 根据传入的callback_url获取request token
	 *
	 * @param backUrl  回调路径
	 * @return         返回得到的requestToken
	 */
    public static RequestToken request(String backUrl) {
		try {
			Weibo weibo = new Weibo();
			RequestToken requestToken = weibo.getOAuthRequestToken(backUrl);

			return requestToken;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	/**
	 * 传入request token 和 verifier 获取access token
	 *
	 * @param requestToken
	 * @param verifier
	 * @return
	 */
	public static AccessToken requstAccessToken(RequestToken requestToken,
			String verifier) {
		try {
			Weibo weibo = new Weibo();
			AccessToken accessToken = weibo.getOAuthAccessToken(requestToken
					.getToken(), requestToken.getTokenSecret(), verifier);

			return accessToken;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

    /**
     * 传入request token和request token secret字符串  和 verifier 获取access token
     *
     * @param requestToken
     * @param requestTokenSecret
     * @param verifier
     * @return
     */
	public static AccessToken requstAccessToken(String requestToken, String requestTokenSecret,
			String verifier) {
		try {
			Weibo weibo = new Weibo();
			AccessToken accessToken = weibo.getOAuthAccessToken(requestToken, requestTokenSecret, verifier);

			return accessToken;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	/**
	 * 根据传入的Access Token 和内容发表微博
	 *
	 * @param access
	 * @param content
	 */
	public static void updateWeibo(AccessToken access, String content) {
		try {
			Weibo weibo = new Weibo();
			weibo.setToken(access.getToken(), access.getTokenSecret());
            weibo.updateStatus(content);
		} catch (WeiboException e) {
			e.printStackTrace();
		}
	}

    /**
     * 发送带图片的微博
     *
     * @param access
     * @param content      微博内容
     * @param imageItem    零时图片对象
     */
    public static void updateWeibo(AccessToken access, String content, ImageItem imageItem) {
        try {
			Weibo weibo = new Weibo();
			weibo.setToken(access.getToken(), access.getTokenSecret());
            weibo.uploadStatus(content, imageItem);
		} catch (WeiboException e) {
			e.printStackTrace();
		}
    }
}
